//
//  MainViewController.swift
//  ZTWeiboo
//
//  Created by mac on 15/8/6.
//  Copyright © 2015年 YYL. All rights reserved.
//≤

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addControllers()
    }
    override func viewDidAppear(animated: Bool) {
        setupComposeButton()
    }
    func clickComposeButton(){
        print("hey")
    }
    private func setupComposeButton(){
        let w = tabBar.bounds.width/CGFloat((viewControllers?.count)!)
        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
    }
    private func addControllers(){
        addController(HomeTableViewController(), title: "主页", imageName: "tabbar_home")
        addController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    private func addController(ViewController:UIViewController ,title: String ,imageName: String){
        tabBar.tintColor = UIColor.orangeColor()
        ViewController.title = title
        ViewController.tabBarItem.image = UIImage(named: imageName)

        let nav = UINavigationController(rootViewController: ViewController)
        
        addChildViewController(nav)
    }
    lazy private var composeBtn : UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        self.tabBar.addSubview(button)
        button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}

